using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Reflection;
using System;

public abstract class Singleton<T> where T:Singleton<T>
{
    protected Singleton() { }

    protected static T instance = null;

    public static T Instance
    {
        get
        {
            if(instance == null)
            {
                ConstructorInfo[] constructorInfos = typeof(T).GetConstructors(BindingFlags.Instance|BindingFlags.NonPublic);
                ConstructorInfo ctor = Array.Find(constructorInfos, c => c.GetParameters().Length == 0);
                if (ctor == null)
                {
                    throw new Exception("Non-public ctor() not found!");
                }
                instance = ctor.Invoke(null) as T;
            }
            return instance;
        }
    }
}
